package com.yf.modules.system.controller;

import com.yf.Utils.PageUtil;
import com.yf.domain.dict.DictDetail;
import com.yf.model.query.DictDetailQueryCriteria;
import com.yf.modules.system.service.api.DictDetailService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2024-10-14 17:36
 * @description 数据字典明细restful
 */
@RestController
@RequestMapping("/api/dictDetail")
@Slf4j
@Tag(name = "DictApi", description = "字典Api")
public class DictDetailController {

    private final DictDetailService dictDetailService;

    public DictDetailController(DictDetailService dictDetailService) {

        this.dictDetailService = dictDetailService;
    }

    @GetMapping("")
    @Operation(summary = "查询字典明细", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> queryDictDetail(DictDetailQueryCriteria detailQueryCriteria) {
        List<DictDetail> dictDetails = dictDetailService.queryDictDetail(detailQueryCriteria);
        return ResponseEntity.ok().body(ResponseBody.success(PageUtil.toPage(dictDetails, dictDetails.size())));
    }

    @PostMapping(value = "/add")
    @Operation(summary = "添加字典明细", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> addDictDetail(@RequestBody DictDetail resources) {
        dictDetailService.addDictDetail(resources);
        return ResponseEntity.ok().body(ResponseBody.success("添加成功"));
    }

    @PutMapping(value = "/edit")
    @Operation(summary = "修改字典明细", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> editDictDetail(@RequestBody DictDetail resources) {
        dictDetailService.editDictDetail(resources);
        return ResponseEntity.ok().body(ResponseBody.success("修改成功"));
    }

    @DeleteMapping(value = "/delete/{pk}")
    @Operation(summary = "删除字典明细", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> deleteDictDetail(@PathVariable Long pk) {
        Set<Long> ids = Set.of(pk);
        dictDetailService.deleteDictDetail(ids);
        return ResponseEntity.ok().body(ResponseBody.success("删除成功"));
    }
}
